import { ComponentPublicInstance } from 'vue';

interface ContainerProps {
  tag?: string | { value?: string; props?: Record<string, unknown> };
}
export function getTagProps<T>(
  ctx: ComponentPublicInstance<ContainerProps & T>,
  tagClass?: string,
) {
  let tag = ctx.$props.tag || 'div';
  let props:
    | {
        [key: string]: unknown;
      }
    | undefined = undefined;

  if (typeof tag === 'string') {
    if (tagClass) {
      props = {
        class: tagClass,
      };
    }
  }

  if (typeof tag === 'object') {
    props = tag.props || {};
    tag = tag.value || 'div';
    if (tagClass) {
      if (props.class) {
        if (Array.isArray(props.class)) {
          props.class.push(tagClass);
        } else {
          props.class = [tagClass, props.class];
        }
      }
    }
  }

  return { value: tag, props };
}


export function validateTagProp(tag: {[key: string]: unknown}): boolean {
  return (
    typeof tag === 'string' ||
    (typeof tag === 'object' &&
        tag !== null &&
      (typeof tag.value === 'string' ||
        typeof tag.value === 'function' || typeof tag.value === 'object'))
  );
}